const express = require('express');
const app = express();

const { proxy, scriptUrl } = require('rtsp-relay')(app);

app.ws('/api/stream/:hostname/:path', (ws, req) => {
  const additionalFlags = req.query.additionalFlags
    ? req.query.additionalFlags.split(',').map(decodeURIComponent)
    : [];

  console.log('additionalFlags: ', additionalFlags);

  const url = `rtsp://${req.params.hostname}/${req.params.path}`
  return proxy({
    url,
    verbose: true,
    useNativeFFmpeg: true,
    additionalFlags,
  })(ws)
});

// 定义默认路由 /
app.get('/', (req, res) => res.send('Stream relay server'))

const PORT = process.env.PORT || 2018
app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`)
})
